<script setup lang="ts">
interface Props {
    show: boolean;
    title: string;
    left: number;
    top: number;
}
const props = withDefaults(defineProps<Props>(), { show: false, title: "", left: 0, top: 0 });
const emits = defineEmits(["update:show", "update:left", "update:top"]);
const mousedown = () => {
    document.body.addEventListener("mousemove", mousemove);
    document.body.addEventListener("mouseup", mouseup);
    function mousemove(e: MouseEvent) {
        emits("update:left", props.left + e.movementX);
        emits("update:top", props.top + e.movementY);
    }
    function mouseup() {
        document.body.removeEventListener("mousemove", mousemove);
        document.body.removeEventListener("mouseup", mouseup);
    }
};
</script>

<template>
    <Teleport to="body">
        <Transition name="fade">
            <dialog v-if="props.show" class="floating-window" :style="{ left: props.left + 'px', top: props.top + 'px' }">
                <h1 @mousedown="mousedown()">
                    <span>{{ title }}</span>
                    <svg class="icon" aria-hidden="true" @click="emits('update:show', false)"><use xlink:href="#icon-guanbi"></use></svg>
                </h1>
                <slot></slot>
            </dialog>
        </Transition>
    </Teleport>
</template>

<style scoped lang="scss">
.floating-window {
    position: fixed;
    padding: 10px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 0 3px 1px var(--primary-color);
    border-radius: 10px;
    z-index: 9;
    backdrop-filter: blur(10px);
    background-color: rgba(255, 255, 255, 0.6);
    h1 {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 30px;
        cursor: move;
        span {
            font-size: 18px;
            font-weight: bold;
        }
        .icon {
            padding: 4px;
            cursor: pointer;
            font-size: 14px;
            background-color: #ccc;
            border-radius: 50%;
        }
    }
}
</style>
